#include <iostream>
#include <vector>
using namespace std;
void duplicateZeros(vector<int> &arr)
{
    int cur = 0;
    int dest = -1;
    int n = arr.size();
    while (cur < n)
    {
        if (arr[cur])
            dest++;
        else
            dest += 2;
        if (dest <= n - 1)
        {
            break;
        }
        cur++;
    }
    if (dest == n)
    {
        arr[n - 1] = 0;
        cur--;
        dest -= 2;
    }
    while (cur >= 0)
    {
        arr[dest] = arr[cur];
        if (arr[cur] == 0)
        {
            arr[--dest] = 0;
        }
        dest--;
        cur--;
    }
}
int main()
{
    vector<int> arr = {1, 0, 2, 3, 0, 4, 5, 0};
    duplicateZeros(arr);
    for (auto e : arr)
    {
        cout << e << endl;
    }
}
